/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.commons.view;

import java.awt.BorderLayout;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Console extends JPanel {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InternalInterface parent;
	private JLabel label;
	private JTextArea output;
	private JTextField prompt;
	private ArrayList<String> history;
	private Point2D lastPoint;
	
//*****************************************************************************
	
	public Console(InternalInterface par) {
		
		super(new BorderLayout(5, 5));
		parent = par;
		
		history = new ArrayList<String>(20);
		lastPoint = new Point2D.Double(0,0);
		
		label = new JLabel(parent.getResourceBundle().getString("gPrompt") + ": ");
		this.add(BorderLayout.WEST, label);
		
		prompt = new JTextField();
		this.add(BorderLayout.CENTER, prompt);
		
		output = new JTextArea(2, 120);
		output.setLineWrap(true);
		output.setEditable(false);
		JScrollPane scroller = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(BorderLayout.NORTH, scroller);
		
		this.setBorder(BorderFactory.createEmptyBorder(2,3,1,3));
	}
	
//*****************************************************************************
		
	public void setOutputText(String str) {
		output.setText(str);
		output.setCaretPosition(output.getText().length());
	};
	
//*****************************************************************************
	
	public void addOutputText(String str) {
		output.append("\n");
		output.append(str);
		output.setCaretPosition(output.getText().length());
	};
	
//*****************************************************************************
	
	public void clearPrompt() {
		prompt.setText("");
	}
	
//*****************************************************************************
	
	public void setPromptText(String str) {
		prompt.setText(str);
	}
	
//*****************************************************************************
	
	public void clear() {
		output.setText("Universidade Federal de Minas Gerais");
		output.append("\nEscola de Engenharia");
		output.setCaretPosition(output.getText().length());
		history.clear();
	}
	
//*****************************************************************************
	
	public void addListener(ConsoleHandler ch) {
		prompt.addActionListener(ch);
		prompt.addKeyListener(ch);
	}
	
//*****************************************************************************
	
	public void requestFocus() {
		prompt.requestFocus();
	}
	
//*****************************************************************************
	
	public void addToHistory(String str) {
		if (history.size() == 0) {history.add(0, str);}
		else {
			String str2 = (String) history.get(0);
			if (!str.equals(str2)) {history.add(0, str);}
			}
	}
	
//*****************************************************************************
	
	public ArrayList getHistory() {
		return history;
	}
	
//*****************************************************************************
	
	public String getLastCommand() {
		String str = (String) this.getHistory().get(0);
		return str;
	}
	
//*****************************************************************************
	
	public Point2D getLastPoint() {
		return lastPoint;
	}
	
//*****************************************************************************
	
	public void setLastPoint(Point2D p) {
		lastPoint = p;
	}

//*****************************************************************************
	
	public void setAllLabels() {
		label.setText(parent.getResourceBundle().getString("gPrompt") + ": ");
	}
	
//*****************************************************************************
}
